/**
 * 登录
 * username
 * password
 */
#include "../myfastdo/myfastdo.hpp"
#include "../protocol_dist.hpp"
#include "../utils.hpp"
USING_ALL;
FASTDO_API void ActionMain(MyFastdoServer &SERV)
{
    User user(SERV.data.REQ);
    // 已经登录了
    if (user.isLogged())
    {
        throw RequestError("ALREADY_LOGGED");
    }

    ProtocolC2SLogin msg;
    msg.fromMixed(SERV.data.postMixed());
    if (msg.username.size() == 0 || msg.password.size() == 0)
    {
        throw RequestError("REQ_ILL");
    }
    // 用户名错误
    if (!HAS_USER(msg.username))
    {
        throw RequestError("NAME_PWD_WRONG");
    }

    Mixed userData = SERV.getDBH("tileblog").query("select * from users where name=?", STATMENTS(msg.username), DBH::One);

    // 密码错误
    if ((String)(userData["password"]) != msg.password)
    {
        throw RequestError("NAME_PWD_WRONG");
    }
    // 登录
    user.login(userData);

    ProtocolS2CLogin rtn;
    rtn.user_data->fromMixed(userData);
    SERV.data.res.data = rtn.toMixed();
}